<template>
  <div>
    <el-card style="margin:20px 0">
      <CatgorySelect @getCategoryId="getCategoryId" :show='scene!=0'></CatgorySelect>
    </el-card>    
    <el-card>
      <!-- 展示Spu列表 -->
      <div v-show="scene==0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80%" align="center"> </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width"> </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width"> </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加Sku" @click="addSku(row)"></hint-button>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改spu" @click="updateSpu(row)"></hint-button>
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前spu全部的sku列表" @click="handler(row)"></hint-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align:center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout=" prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
      <SpuFrom v-show="scene==1" @changeScene="changeScene" ref="spu"></SpuFrom>
      <SkuFrom v-show="scene==2" @changeScenes="changeScenes" ref="sku"></SkuFrom>
      <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close='close'>
        <!-- table展示sku列表 -->
        <el-table :data="skuList" style="width: 100%" border v-loading="loading">
          <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
          <el-table-column prop="price" label="价格" width="width"></el-table-column>
          <el-table-column prop="weight" label="重量" width="width"></el-table-column>
          <el-table-column prop="prop" label="默认图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.skuDefaultImg" alt="" style="wigth:100px;height:100px">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// 引入子组件
import SpuFrom from './SpuFrom'
import SkuFrom from './SkuFrom'
export default {
  name: 'Spu',
  data() {
    return {
      category1Id:'',
      category2Id:'',
      category3Id:'',
      page: 1,   // 分页器代表当前第一页
      limit:3,     // 每一页展示数
      records: [],
      total:0,
      scene:0,     // 0代表展示spu列表的数据   1代表展示添加spu|修改spu   2代表展示添加sku
      dialogTableVisible:false,       // 控制对话框的显示与隐藏
      spu: {},
      skuList: [],              // 存储sku列表的数据
      loading: true            // 添加loading效果
    }
  },
  methods: {
    // 三级联动的自定义事件，可以把子组件的相应的id传递给父组件
    getCategoryId({categoryId,level}) {
      if(level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if(level == 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else if(level == 3) {
        this.category3Id = categoryId
        // 获取SPU列表数据展示
        this.getSpuList()
      }
    },
    // 获取Spu列表数据
    async getSpuList(pages = 1) {
      // 点击分页器第几页赋值，之后发请求。默认第一页
      this.page = pages
      // 解构参数：page 第几页； limit  每一页展示多少条数据； 三级分类的id
      const {page,limit,category3Id} = this
      let result = await this.$API.spu.reqSpuList(page,limit,category3Id)
      if(result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    // 当分页器某一页展示数据发生变化的回调
    handleSizeChange(limit) {
      // 修改参数再次发请求
      this.limit = limit
      this.getSpuList()
    },
    // 添加Spu
    addSpu() {
      // 切换场景为1
      this.scene = 1
      // 通知子组件spuFrom发请求----两个请求，获取品牌与销售属性
      this.$refs.spu.addSpuDate(this.category3Id)
    },
    // 修改Spu
    updateSpu(row) {
      this.scene = 1
      // 获取子组件SpuFrom子组件的数据与方法等
      // 在父组件当中可以通过$this.$refs获取子组件等等
      this.$refs.spu.initSpuData(row)
    },
    // SpuFrom自定义事件回调
    changeScene({scene,flag}) {
      // 切换场景解构
      this.scene = scene
      // 子组件通知父组件切换场景，需要再次获取spu列表数据进行展示
      if(flag=='修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    // 删除spu的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if(result.code == 200) {
        this.$message({type: "success", message: "删除成功"})
        // 代表删除的时候SPU个数大于1，停留在当前页，小于1回到上一页
        this.getSpuList(this.records.length>1?this.page:this.page-1)
      }
    },
    // 添加Sku按钮回调
    addSku(row) {
      this.scene = 2
      // 父组件调用子组件的方法，让子组件发请求获取数据（三个请求）
      this.$refs.sku.getDate(this.category1Id,this.category2Id,row)
    },
    // skuForm父组件切换场景
    changeScenes(scene) {
      this.scene = scene
    },
    // 查看sku按钮的回调
    async handler(spu) {
      // 保存spu的信息
      this.spu = spu
      // 当点击按钮的时候显示对话框
      this.dialogTableVisible = true
      // 获取sku列表数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id)
      if(result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    // 关闭对话框的回调
    close(done) {
      this.loading = true
      // 清除sku列表的数据
      this.skuList = []
      done()
    }
  },
  components: {
    SpuFrom,
    SkuFrom
  }
}
</script>

<style>

</style>
